package sdc.media.motorbike.View;

import sdc.media.motorbike.MotorBike;
import sdc.media.motorbike.Controller.Assets;
import sdc.media.motorbike.Controller.Setting;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class MenuScreen extends BaseScreen {

	// Background and button, etc...
	private Image imgBack;
	private Image buttonPlay;
	private Image buttonSelectMotor;
	private Image buttonHighScore;
	private Image buttonGuide;
	private Image buttonExit;
	
	private boolean isPlayClick;
	private boolean isSelectMotorClick;
	private boolean isHighScoreClick;
	private boolean isGuideClick;
	private boolean isExitClick;
	
	private float sizeX;
	private float sizeY;

	@Override
	public void render(float delta) {
		super.render(delta);
		batch.begin();
		batch.draw(Assets.logo, 5 * sizeX, 600 * sizeY, Assets.logo.getRegionWidth() * sizeX, Assets.logo.getRegionHeight() * sizeY);
		if(isPlayClick)
			batch.draw(Assets.buttonPlay_click, 150 * sizeX, 500 * sizeY, Assets.buttonPlay_click.getRegionWidth() * sizeX, Assets.buttonPlay_click.getRegionHeight() * sizeY);
		else
			batch.draw(Assets.buttonPlay, 150 * sizeX, 500 * sizeY, Assets.buttonPlay.getRegionWidth() * sizeX, Assets.buttonPlay.getRegionHeight() * sizeY);
		
		if(isSelectMotorClick)
			batch.draw(Assets.buttonSelectMotor_click, 100 * sizeX, 400 * sizeY, Assets.buttonSelectMotor_click.getRegionWidth() * sizeX, Assets.buttonSelectMotor_click.getRegionHeight() * sizeY);
		else
			batch.draw(Assets.buttonSelectMotor, 100 * sizeX, 400 * sizeY, Assets.buttonSelectMotor.getRegionWidth() * sizeX, Assets.buttonSelectMotor.getRegionHeight() * sizeY);
		
		if(isHighScoreClick)
			batch.draw(Assets.buttonHighScore_click, 150 * sizeX, 300 * sizeY, Assets.buttonHighScore_click.getRegionWidth() * sizeX, Assets.buttonHighScore_click.getRegionHeight() * sizeY);
		else
			batch.draw(Assets.buttonHighScore, 150 * sizeX, 300 * sizeY, Assets.buttonHighScore.getRegionWidth() * sizeX, Assets.buttonHighScore.getRegionHeight() * sizeY);
		
		if(isGuideClick)
			batch.draw(Assets.buttonGuide_click, 150 * sizeX, 200 * sizeY, Assets.buttonGuide_click.getRegionWidth() * sizeX, Assets.buttonGuide_click.getRegionHeight() * sizeY);
		else
			batch.draw(Assets.buttonGuide, 150 * sizeX, 200 * sizeY, Assets.buttonGuide.getRegionWidth() * sizeX, Assets.buttonGuide.getRegionHeight() * sizeY);
		
		if(isExitClick)
			batch.draw(Assets.buttonExit_click, 150 * sizeX, 100 * sizeY, Assets.buttonExit_click.getRegionWidth() * sizeX, Assets.buttonExit_click.getRegionHeight() * sizeY);
		else
			batch.draw(Assets.buttonExit, 150 * sizeX, 100 * sizeY, Assets.buttonExit.getRegionWidth() * sizeX, Assets.buttonExit.getRegionHeight() * sizeY);
		
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}

	@Override
	public void show() {
		super.show();
		
		// Constructor
		sizeX = Gdx.graphics.getWidth() / 480.0f;
		sizeY = Gdx.graphics.getHeight() / 800.0f;
		imgBack = new Image(Assets.backgroundMenu);
		imgBack.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		stage.addActor(imgBack);
		
		buttonPlay = new Image();
		buttonPlay.setSize(Assets.buttonPlay.getRegionWidth() * sizeX, Assets.buttonPlay.getRegionHeight() * sizeY);
		buttonPlay.setPosition(150 * sizeX, 500 * sizeY);
		stage.addActor(buttonPlay);
		
		buttonSelectMotor = new Image();
		buttonSelectMotor.setSize(Assets.buttonSelectMotor.getRegionWidth() * sizeX, Assets.buttonSelectMotor.getRegionHeight() * sizeY);
		buttonSelectMotor.setPosition(150 * sizeX, 400 * sizeY);
		stage.addActor(buttonSelectMotor);
		
		buttonHighScore = new Image();
		buttonHighScore.setSize(Assets.buttonHighScore.getRegionWidth() * sizeX, Assets.buttonHighScore.getRegionHeight() * sizeY);
		buttonHighScore.setPosition(150 * sizeX, 300 * sizeY);
		stage.addActor(buttonHighScore);
		
		buttonGuide = new Image();
		buttonGuide.setSize(Assets.buttonGuide.getRegionWidth() * sizeX, Assets.buttonGuide.getRegionHeight() * sizeY);
		buttonGuide.setPosition(150 * sizeX, 200 * sizeY);
		stage.addActor(buttonGuide);
		
		buttonExit = new Image();
		buttonExit.setSize(Assets.buttonExit.getRegionWidth() * sizeX, Assets.buttonExit.getRegionHeight() * sizeY);
		buttonExit.setPosition(150 * sizeX, 100 * sizeY);
		stage.addActor(buttonExit);

		if (Setting.soundEnabled) {
			// Play music and sound
			Assets.backgroundPlayMusic.play();
		} else {
			//Pause music and sound
			Assets.backgroundPlayMusic.pause();
		}

		// Button Event...
		buttonPlay.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isPlayClick = false;
				MotorBike.getManagerScreen().createScreen(
						ManagerScreen.SCREEN_PLAY);
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				Assets.playSound(Assets.buttonClick);
				isPlayClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
		buttonSelectMotor.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isSelectMotorClick = false;
				MotorBike.getManagerScreen().createScreen(
						ManagerScreen.SCREEN_SELECT_MOTOR);
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				Assets.playSound(Assets.buttonClick);
				isSelectMotorClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
		buttonHighScore.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isHighScoreClick = false;
				MotorBike.getManagerScreen().createScreen(
						ManagerScreen.SCREEN_HIGH_SCORE);
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				Assets.playSound(Assets.buttonClick);
				isHighScoreClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
		buttonGuide.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isGuideClick = false;
				MotorBike.getManagerScreen().createScreen(
						ManagerScreen.SCREEN_GUIDE);
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				Assets.playSound(Assets.buttonClick);
				isGuideClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
		buttonExit.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				isExitClick = false;
				Assets.dispose();
				Gdx.app.exit();
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				Assets.playSound(Assets.buttonClick);
				isExitClick = true;
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
	}

}
